package com.vgooo.client.myvg.tencent;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.open.HttpStatusException;
import com.tencent.open.NetworkUnavailableException;
import com.tencent.tauth.Constants;
import com.tencent.tauth.IRequestListener;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.vgooo.client.VGActivity;
import com.vgooo.client.myvg.OtherLogin;
import com.vgooo.client.myvg.OtherLogin.LoginListener;

public class QQOauthorizeAction  {
	
	private final static String calssName = "QQOathActivity" ;
	private final static String APP_ID = "100225737" ;
	
	
//	private static final String SCOPE = "get_user_info,add_t,add_pic_t";
	private static final String SCOPE = "all";

	private Tencent tencent ;
	
	private Activity activity ;
	
	private String tag ;
	
	private LoginListener listener ;
	
	public static boolean  isLogin = false ;
	
	
	
	public QQOauthorizeAction(Activity activity, String tag,LoginListener listener ) {
		this.activity = activity;
		this.tag = tag;
		this.listener = listener ;
		init() ;
	}

	
	
//	public QQOauthorizeAction(VGActivity cparent, String className,
//			LoginListener loginListener) {
//		// TODO Auto-generated constructor stub
//	}



	public Tencent getTencent() {
		return tencent;
	}



	private  void init() {
		// TODO Auto-generated method stub
		tencent = Tencent.createInstance(APP_ID, activity.getApplicationContext());
//		if(!tencent.isSessionValid()){
			IUiListener mListener = new BaseUiListener(){
				@Override
				protected void doComplete(JSONObject values) {
					// TODO Auto-generated method stub
					super.doComplete(values);
					Toast.makeText(activity, "QQ授权成功", Toast.LENGTH_SHORT).show();
					isLogin = true ;
					OtherLogin.OtherLogin(activity, tencent.getOpenId(), OtherLogin.TYPE_QQ, listener);
				}
			};
			tencent.login(activity, SCOPE, mListener);
//		}
//		else{
//			tencent.requestAsync(Constants.GRAPH_OPEN_ID, null, Constants.HTTP_GET, new BaseApiListener(), null);
//			tencent.logout(this);
//		}
	}
	
	
	private class BaseApiListener implements IRequestListener{

		@Override
		public void onComplete(JSONObject response, Object state) {
			// TODO Auto-generated method stub
			doComplete(response, state);
		}
		
		protected void doComplete(JSONObject response , Object state){
			
		}

		@Override
		public void onIOException(IOException e, Object state) {
			// TODO Auto-generated method stub
		}
		
		@Override
		public void onMalformedURLException(MalformedURLException e,
				Object state) {
			// TODO Auto-generated method stub
		}
		
		@Override
		public void onJSONException(JSONException e, Object state) {
			// TODO Auto-generated method stub
		}
		
		@Override
		public void onConnectTimeoutException(ConnectTimeoutException e,
				Object state) {
			// TODO Auto-generated method stub
		}

		
		@Override
		public void onSocketTimeoutException(SocketTimeoutException e,
				Object state) {
			// TODO Auto-generated method stub
		}
		
		@Override
		public void onHttpStatusException(HttpStatusException e, Object state) {
			// TODO Auto-generated method stub
		}


		@Override
		public void onNetworkUnavailableException(
				NetworkUnavailableException e, Object state) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onUnknowException(Exception e, Object state) {
			// TODO Auto-generated method stub
		}
		
	}
	
	private class BaseUiListener implements IUiListener {


		@Override
		public void onComplete(JSONObject response) {
			// TODO Auto-generated method stub
			doComplete(response);
		}

		protected void doComplete(JSONObject values){
			
		}
		
		@Override
		public void onError(UiError e) {
			// TODO Auto-generated method stub
		}
		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
		}
		
	}
	

}
